package com.xjc.business.controller.schadmin;

import com.xjc.business.entity.po.schadmin.CollegePo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.schadmin.CollegeVo;
import com.xjc.business.service.CollegesService;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 徐锦程
 * 时间:2025-01-27 14:48
 */
@RestController("sch_admin_college_controller")
@RequestMapping("/sch_admin/college")
public class CollegeController {

    @Autowired
    CollegesService collegesService;

    /**
     * 获取学院分页列表
     * @param pageSize 每页条数
     * @param currentPage 当前页码
     * @param schoolId 学校id
     * @param collegeName 学院名称
     * @return 列表数据
     */
    @GetMapping("/page/{pageSize}/{currentPage}")
    public Result<PageVo<CollegeVo>> getCollegePage(
            @PathVariable(value = "currentPage", required = false)  Integer currentPage,
            @PathVariable(value = "pageSize", required = false) Integer pageSize,
            Integer schoolId,
            String collegeName
    ){
        if(schoolId == null || schoolId <= 0){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }
        if(pageSize == null || pageSize <= 0){
            pageSize = 10;
        }
        if(currentPage == null || currentPage <= 0){
            currentPage = 1;
        }

        return collegesService.getCollegePage(pageSize,currentPage, schoolId, collegeName);
    }

    /**
     * 添加学院
     * @param collegePo 学院信息
     * @return 反馈
     */
    @PostMapping("/add")
    public Result<String> addCollege(@RequestBody(required = false)CollegePo collegePo){
        if (collegePo == null || collegePo.getSchoolId() == null || collegePo.getPic().isEmpty() || collegePo.getDesc().isEmpty() || collegePo.getName().isEmpty()){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return collegesService.addCollege(collegePo);
    }

    /**
     * 分配管理员
     * @param adminId 管理员id
     * @param collegeId 学院id
     * @return 反馈
     */
    @PostMapping("/associate_admin/{adminId}/{collegeId}")
    public Result<String> associateAdmin(
            @PathVariable(value = "adminId", required = false)  Integer adminId,
            @PathVariable(value = "collegeId", required = false) Integer collegeId
    ){
        if(adminId == null || collegeId == null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.PARAM_MISSING);
        }

        return collegesService.associateAdmin(adminId,collegeId);
    }
}
